package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import autoteam.tables.AutoTeamTableModel;
import db.ColumnData;
import db.DataAccessObject;

public class LeagueCreator extends AutoTeamDialog
{
	private static final long serialVersionUID = 5775532387293650231L;

	private JButton addButton = new JButton("Add");
	private JButton cancelButton = new JButton("Cancel");

	private JTextField leagueField;

	private JTextField sportField;
	
	private AutoTeamTableModel model;

	public LeagueCreator(JFrame dialogOwner)	//DO NOT USE
	{
		super(dialogOwner);
		this.setTitle("Add New League");
		this.setDialogSize(new Dimension(300, 125));

		JPanel buttonPane = new JPanel();
		buttonPane.add(addButton);
		buttonPane.add(cancelButton);

		JPanel formPane = new JPanel(new GridLayout(2, 2));
		JLabel leagueName = new JLabel("League Name: ");
		JLabel sportType = new JLabel("Sport Type: ");

		leagueField = new JTextField(25);
		sportField = new JTextField(25);

		formPane.add(leagueName);
		formPane.add(leagueField);
		formPane.add(sportType);
		formPane.add(sportField);

		Container contentPane = getContentPane();
		setButtonActions(this);
		contentPane.add(formPane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.SOUTH);

		pack();
	}
	
	public LeagueCreator(JFrame dialogOwner, AutoTeamTableModel model)
	{
		this(dialogOwner);
		this.model = model;
	}

	protected void setButtonActions(final AutoTeamDialog thisDialog)
	{
		addButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				DataAccessObject dao = new DataAccessObject();
				dao.addRecord("leagues", new ColumnData("leagueName",
						leagueField.getText()), new ColumnData("sport",
						sportField.getText()));
				model.add(0);
				model.fireTableDataChanged();
				thisDialog.close();
			}
		});

		cancelButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				thisDialog.close();
			}
		});
	}

}
